<?php
/**
 * | ---------------------------------------------------------------------------------------------------
 * | Author：johnxu <fsyzxz@163.com>.
 * | ---------------------------------------------------------------------------------------------------
 * | Home: https://www.johnxu.net.
 * | ---------------------------------------------------------------------------------------------------
 * | Data: 2019-03-16
 * | ---------------------------------------------------------------------------------------------------
 * | Desc: 客服管理
 * | ---------------------------------------------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\UsersGroup;
use app\common\controller\Backend;
use app\admin\model\Users as UsersModel;
use app\admin\validate\Users as UsersValidate;
use app\admin\model\Group as GroupModel;
use johnxu\tool\Hash;

/**
 * 客服管理
 *
 * Class Service
 *
 * @package app\admin\controller
 */
class Service extends Backend
{
    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->assign( 'rows', UsersModel::getUserList() );

        return $this->fetch();
    }

    /**
     * 添加客服
     *
     * @return mixed
     */
    public function add()
    {
        if ( $this->request->isPost() )
        {
            $data          = $this->request->post();
            $groupValidate = new UsersValidate();
            if ( !$groupValidate->scene( 'add' )->check( $data ) )
            {
                $this->error( $groupValidate->getError() );
            }
            else
            {
                $data['type'] = 'service';
                $result       = UsersModel::create( $data );
                $this->updateGroupId( $result['id'], $data['group_id'] );

                $result ? $this->success( '添加客服成功', url( 'index' ) ) : $this->error( '添加客服失败' );
            }
        }
        else
        {
            $this->assign( 'groups', GroupModel::scope( 'normal' )->select() );

            return $this->fetch();
        }
    }

    /**
     * 编辑功能
     *
     * @param string $uid
     *
     * @return mixed
     */
    public function edit( string $uid )
    {
        $row = UsersModel::get( [ 'uid' => $uid, 'type' => 'service' ] );
        if ( !$row )
        {
            $this->error( '错误的客服编号' );
        }
        $row['group_id'] = UsersGroup::getGroupIdByUsersId( $row['id'] );
        if ( $this->request->isPost() )
        {
            $data          = $this->request->post();
            $groupValidate = new UsersValidate();
            if ( !$groupValidate->scene( 'edit' )->check( $data ) )
            {
                $this->error( $groupValidate->getError() );
            }

            $row->username = $data['username'];
            $row->status   = $data['status'];
            if ( $data['password'] && !empty( $data['password'] ) )
            {
                $row->password = Hash::make( $data['password'] );
            }
            $this->updateGroupId( $row->id, $data['group_id'] );
            $row->save() !== false ? $this->success( '编辑客服成功', url( 'index' ) ) : $this->error( '编辑客服失败' );
        }
        else
        {
            return $this->fetch( '', [ 'row' => $row, 'groups' => GroupModel::scope( 'normal' )->select() ] );
        }
    }

    /**
     * 删除客服
     *
     * @param string $uid
     */
    public function delete( string $uid )
    {
        $result = UsersModel::destroy( [ 'uid' => $uid, 'type' => 'service' ] );

        $result ? $this->success( '删除客服成功' ) : $this->error( '删除客服失败' );
    }

    /**
     * 更新分组
     *
     * @param $userId
     * @param $groupId
     *
     * @return mixed
     */
    protected function updateGroupId( $userId, $groupId )
    {
        $usersGroup = UsersGroup::get( [ 'users_id' => $userId ] );
        if ( $usersGroup )
        {
            $usersGroup->group_id = $groupId;
            $result               = $usersGroup->save();
        }
        else
        {
            $result = UsersGroup::create( [
                'users_id' => $userId,
                'group_id' => $groupId
            ] );
        }

        return $result;
    }
}
